import { FILE_TIME_OUT, get, post } from "@/common/useAxios";

export interface ISliceUploadFile {
  fileName: string; // 文件名称
  index: number; // 当前片数
  lastModified: string;
  total: number; //总片数
  filepathname: string; // 文件保存的文件夹
  data: any; //文件片
  filesize: number;
}

/**
 * 分片上传
 * @param data
 */
export const sliceUploadFile = (data: ISliceUploadFile) => {
  const formData = new FormData();
  Object.entries(data).map(([key, val]) => {
    formData.append(key, val + "");
  });
  return post("upload/UploadChunk", data, {
    baseURL: SYSTEM_GLOBAL.fileBaseURL,
    headers: {
      "Content-Type": "multipart/form-data",
    },
    timeout: FILE_TIME_OUT,
  });
};

/**
 * 获取附件文件预览的页面地址
 * @param tid 文件tid
 */
export const getAnnexPreviewPath = (tid: string) => {
  return get(
    "Project/ProSubTab/AddrGenerPreview",
    {
      tid,
    },
    {
      headers: {
        // responseType: 'arraybuffer',
        // baseURL: "/down/",
        contentType: "application/octet-stream",
      },
      timeout: FILE_TIME_OUT,
    }
  );
};

/**
 * 获取附件文件预览的页面地址
 * @param path 文件路径
 */
export const getGenerateViewerPath = (path: string, sid: string) => {
  return get(
    "/upload/GetPreviewAddr",
    {
      // path,
      sid,
    },
    {
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

/**
 * 保存预览文件路径
 * @param tid 文件tid
 * @param viewerurl 预览文件路径
 */
export const saveViewerUrl = (tid: string, viewerurl: string) => {
  return get(
    "FileCommon/saveViewerUrl",
    {
      tid,
      viewerurl,
    },

    {
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

/**
 * 下载文件
 */
export const downloadAccessory = (dz: string) => {
  return get(
    dz,
    {},
    {
      responseType: "blob",
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );

  return get(
    "Common/DownloadAcessory_Path",
    { path: dz },

    {
      responseType: "blob",
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

/**
 * 下载压缩包
 * @param tids tid 组成的数组
 */
export const downloadCompressFiles = (tids: string[]) => {
  return post(
    "Common/DownloadCompressFiles",
    { tids: tids ? tids.join(",") : "" },

    {
      responseType: "blob",
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

/**
 * 根据项目/合同tid下载
 * @param tids 项目/合同tid 组成的数组
 * @param type  类型 1项目 2合同
 * @param file_type 文件类型 对应flgl
 */
export const downloadHistoryCompressFiles = (
  tids: string[],
  type?: number,
  file_type?: number
) => {
  return post(
    "Common/DownloadHistoryCompressFiles",
    { tids: tids ? tids.join(",") : "", type, file_type },

    {
      responseType: "blob",
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

/**
 * 删除文件
 * @param fileItem
 */
export const deleteFile = (fileItem: any) => {
  const { tid, fjfl } = fileItem;
  // type 文件类型  0 非工程文件 1 合同上传工程  2 子项上传工程 3 项目层级的工程文件
  return post(
    "FileCommon/delAcessoryInfo",
    { tid },

    {
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

/**
 * 批量删除
 * @param tids tid 组成的数组
 */
export const deleteFiles = (tids: string[]) => {
  return post(
    "FileCommon/delBatchAcessoryInfo",
    { tids: tids ? tids.join(",") : "" },

    {
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

// 判断文件是否有下载权限
export const getfilepower = (fileid: string) => {
  return get(
    "Common/common/getfilepower",
    { fileid },
    {
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: FILE_TIME_OUT,
    }
  );
};

//获取文件流
export const getFileStream = (filepath: string) => {
  const path = filepath.replace?.(SYSTEM_GLOBAL.fileBaseURL, "");
  return post(
    "Common/DownloadPhysicalFile",
    {
      filepath: path,
    },
    {
      responseType: "blob",
      baseURL: SYSTEM_GLOBAL.fileBaseURL,
      timeout: 1000 * 60 * 15,
    },
    true
  );
};
